package com.SinaMicrobloggingApi.Configuration;

import java.io.File;
import java.io.InputStream;
import java.util.*;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SinaAPIUrlConfig {

	private static final String APPKEY = "AppKey";
	private static final String APPSECRET = "AppSecret";
	private static final String BASEURL = "BaseUrl";
	private static String appKey;
	private static String appSecret;
	private static String baseUrl;
	private static Dictionary<String, String> configs;

	public static Dictionary<String, String> getConfigs() {
		return configs;
	}

	public static void initCofigruation(InputStream is) {
		SAXParserFactory factory = SAXParserFactory.newInstance();
		try {
			SAXParser parser = factory.newSAXParser();
			SianUrlConfigHandler handler = new SianUrlConfigHandler();
			parser.parse(is, handler);
			configs = handler.getCofings();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static String getAppKey() {
		if (appKey == null) {
			appKey = configs.get(APPKEY);
		}

		return appKey;
	}

	public static String getAppSecret() {
		if (appSecret == null) {
			appSecret = configs.get(APPSECRET);
		}

		return appSecret;
	}

	public static String getBaseUrl() {
		if (baseUrl == null) {
			baseUrl = configs.get(BASEURL);
		}
		return baseUrl;
	}

	public static SinaAPIUrl getSinaAPIUrlByName(String name) {
		String value = configs.get(name);
		SinaAPIUrl url = null;
		if (value != null) {
			url = new SinaAPIUrl();
			url.setName(name);
			url.setUrl(value);
		}

		return url;
	}
}

class SianUrlConfigHandler extends DefaultHandler {
	private Dictionary<String, String> configs;;
	private StringBuilder builder;
	private final static String PROPERTY = "property";
	private final static String NAME = "name";
	private final static String VALUE = "value";
	private SinaAPIUrl currentCofig;

	public Dictionary<String, String> getCofings() {
		return configs;
	}

	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.startDocument();
		configs = new Hashtable<String, String>();
		builder = new StringBuilder();
	}

	@Override
	public void startElement(String arg0, String arg1, String arg2,
			Attributes arg3) throws SAXException {
		// TODO Auto-generated method stub
		super.startElement(arg0, arg1, arg2, arg3);
		if (arg2.equalsIgnoreCase(PROPERTY)) {
			this.currentCofig = new SinaAPIUrl();
			this.currentCofig.setName(arg3.getValue(NAME));
			this.currentCofig.setUrl(arg3.getValue(VALUE));
		}
	}

	@Override
	public void endElement(String arg0, String arg1, String arg2)
			throws SAXException {
		// TODO Auto-generated method stub
		super.endElement(arg0, arg1, arg2);
		if (this.configs != null) {
			if (arg2.equalsIgnoreCase(PROPERTY)) {
				configs.put(currentCofig.getName(), currentCofig.getUrl());
			}
			builder.setLength(0);
		}
	}

	@Override
	public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
		// TODO Auto-generated method stub
		super.characters(arg0, arg1, arg2);
		builder.append(arg0, arg1, arg2);
	}
}
